package com.css.zfzx.fxpg.common.init.controller;

import com.css.zfzx.fxpg.common.init.Initial;
import com.css.zfzx.fxpg.common.init.service.InitialService;
import com.css.zfzx.fxpg.common.utils.EasyPoiExcelUtil;
import com.css.zfzx.fxpg.modules.casulaty.estimate.repository.entity.FxCasulatyEstimateEntity;
import com.css.zfzx.fxpg.modules.casulaty.estimate.viewobjects.FxCasulatyEstimateManageVO;
import com.css.zfzx.fxpg.modules.casulaty.probability.repository.entity.FxCasulatyProbabilityEntity;
import com.css.zfzx.fxpg.modules.economicloss.middle.repository.entity.FxEconomyLossMiddleEntity;
import com.css.zfzx.fxpg.modules.economicloss.probability.repository.entity.FxEconomyLossProbabilityComputeEntity;
import com.css.zfzx.fxpg.modules.middle.repository.entity.FxMiddleResultEntity;
import com.css.zfzx.fxpg.modules.percent.casualty.repository.entity.FxCasualtyPercentEntity;
import com.css.zfzx.fxpg.modules.percent.economicloss.repository.entity.FxEconomicLossEntity;
import com.css.zfzx.fxpg.modules.risk.repository.entity.FxRiskResultEntity;
import com.css.zfzx.fxpg.modules.vulnerability.danger.repository.entity.FxVulnerabilityDangerEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author leon
 * @Date: 2021/2/2 2:10 下午
 */
@RequestMapping("/initial")
@RestController
public class InitialController {

    @Autowired
    InitialService initialService;

    /**
     * 伤亡估计
     * 将上万条数据导出到Excel
     *
     * @param response
     */
    @GetMapping("/ce/export")
    public void exportExcelCE(HttpServletResponse response) {
        try {
            Date fromDate = new Date();
            List<FxCasulatyEstimateEntity> list = initialService.listInitializeCE();

            Date toDate = new Date();
            long from = fromDate.getTime();
            long to = toDate.getTime();
            int minutes = (int) ((to - from) / (1000 * 60));
            System.out.println("两个时间之间的分钟差为：" + minutes);

            ArrayList<FxCasulatyEstimateManageVO> vos = new ArrayList<>();
            for (FxCasulatyEstimateEntity casulatyEstimateEntity : list) {
                FxCasulatyEstimateManageVO vo = new FxCasulatyEstimateManageVO();
                BeanUtils.copyProperties(casulatyEstimateEntity, vo);
                vos.add(vo);
            }
            EasyPoiExcelUtil.exportExcel(vos, "人员伤亡中间结果管理", "人员伤亡中间结果管理", FxCasulatyEstimateManageVO.class, "人员伤亡中间结果管理.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 伤亡估计
     * 批量新增
     */
    @GetMapping("/ce/saveBatch")
    public void saveBatchCE() {
        try {
            // psBatch 时间统计 - 开始
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date fromDate = new Date();

            List<FxCasulatyEstimateEntity> list = initialService.listInitializeCE();

            Date toDate = new Date();
            long from = fromDate.getTime();
            long to = toDate.getTime();
            int minutes = (int) ((to - from) / (1000 * 60));
            System.out.println("两个时间之间的分钟差为：" + minutes);

            System.out.println("//===================================//");

            Date startDate = new Date();
            String startTime = sdf.format(startDate);
            System.out.println("Batch 开始时间为：" + startTime);
            System.out.println("Batch 开始执行...");

            // 使用PreparedStatement批量插入
            int idx = Initial.sqlBatchCE(list);

            // 统计时间 - 结束
            System.out.println("Batch 执行完成，共插入" + idx + "条数据");
            Date endDate = new Date();
            String endTime = sdf.format(endDate);
            System.out.println("Batch 结束时间为：" + endTime);

            long start = startDate.getTime();
            long end = endDate.getTime();
            int minutes2 = (int) ((end - start) / (1000 * 60));
            System.out.println("两个时间之间的分钟差为：" + minutes2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 重置单价
     */
    @GetMapping("/el/saveBatch")
    public void saveBatchEL() {
        try {
            // psBatch 时间统计 - 开始
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<FxEconomicLossEntity> list = initialService.listInitializeEL();

            Date startDate = new Date();
            String startTime = sdf.format(startDate);
            System.out.println("Batch 开始时间为：" + startTime);
            System.out.println("Batch 开始执行...");

            // 批量插入
            int idx = Initial.sqlBatchEL(list);

            // 统计时间 - 结束
            System.out.println("Batch 执行完成，共插入" + idx + "条数据");
            Date endDate = new Date();
            String endTime = sdf.format(endDate);
            System.out.println("Batch 结束时间为：" + endTime);

            long start = startDate.getTime();
            long end = endDate.getTime();
            int minutes2 = (int) ((end - start) / (1000 * 60));
            System.out.println("两个时间之间的分钟差为：" + minutes2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 人员伤亡百分比
     */
    @GetMapping("/cp/saveBatch")
    public void saveBatchCP() {
        try {
            // psBatch 时间统计 - 开始
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<FxCasualtyPercentEntity> list = initialService.listInitializeCP();

            Date startDate = new Date();
            String startTime = sdf.format(startDate);
            System.out.println("Batch 开始时间为：" + startTime);
            System.out.println("Batch 开始执行...");

            // 使用PreparedStatement批量插入
            int idx = Initial.sqlBatchCP(list);

            // 统计时间 - 结束
            System.out.println("Batch 执行完成，共插入" + idx + "条数据");
            Date endDate = new Date();
            String endTime = sdf.format(endDate);
            System.out.println("Batch 结束时间为：" + endTime);

            long start = startDate.getTime();
            long end = endDate.getTime();
            int minutes2 = (int) ((end - start) / (1000 * 60));
            System.out.println("两个时间之间的分钟差为：" + minutes2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 伤亡概率
     */
    @GetMapping("/cpro/saveBatch")
    public void saveBatchCPro() {
        try {
            // psBatch 时间统计 - 开始
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            List<FxCasulatyProbabilityEntity> list = initialService.listInitializeCPro();


            Date startDate = new Date();
            String startTime = sdf.format(startDate);
            System.out.println("Batch 开始时间为：" + startTime);
            System.out.println("Batch 开始执行...");

            // 使用PreparedStatement批量插入
            int idx = Initial.sqlBatchCPro(list);

            // 统计时间 - 结束
            System.out.println("Batch 执行完成，共插入" + idx + "条数据");
            Date endDate = new Date();
            String endTime = sdf.format(endDate);
            System.out.println("Batch 结束时间为：" + endTime);

            long start = startDate.getTime();
            long end = endDate.getTime();
            int minutes2 = (int) ((end - start) / (1000 * 60));
            System.out.println("两个时间之间的分钟差为：" + minutes2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 房屋抗震能力
     */
    @GetMapping("/mr/saveBatch")
    public void saveBatchMR() {
        try {
            // psBatch 时间统计 - 开始
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            List<FxMiddleResultEntity> list = initialService.listInitializeMR();


            Date startDate = new Date();
            String startTime = sdf.format(startDate);
            System.out.println("Batch 开始时间为：" + startTime);
            System.out.println("Batch 开始执行...");

            // 使用PreparedStatement批量插入
            int idx = Initial.sqlBatchMR(list);

            // 统计时间 - 结束
            System.out.println("Batch 执行完成，共插入" + idx + "条数据");
            Date endDate = new Date();
            String endTime = sdf.format(endDate);
            System.out.println("Batch 结束时间为：" + endTime);

            long start = startDate.getTime();
            long end = endDate.getTime();
            int minutes2 = (int) ((end - start) / (1000 * 60));
            System.out.println("两个时间之间的分钟差为：" + minutes2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 经济损失估计
     */
    @GetMapping("/elm/saveBatch")
    public void saveBatchELM() {
        try {
            // psBatch 时间统计 - 开始
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<FxEconomyLossMiddleEntity> list = initialService.listInitializeELM();

            Date startDate = new Date();
            String startTime = sdf.format(startDate);
            System.out.println("Batch 开始时间为：" + startTime);
            System.out.println("Batch 开始执行...");

            // 使用PreparedStatement批量插入
            int idx = Initial.sqlBatchELM(list);

            // 统计时间 - 结束
            System.out.println("Batch 执行完成，共插入" + idx + "条数据");
            Date endDate = new Date();
            String endTime = sdf.format(endDate);
            System.out.println("Batch 结束时间为：" + endTime);

            long start = startDate.getTime();
            long end = endDate.getTime();
            int minutes2 = (int) ((end - start) / (1000 * 60));
            System.out.println("两个时间之间的分钟差为：" + minutes2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 经济损失概率
     */
    @GetMapping("/elpc/saveBatch")
    public void saveBatchELPC() {
        try {
            // psBatch 时间统计 - 开始
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<FxEconomyLossProbabilityComputeEntity> list = initialService.listInitializeELPC();

            Date startDate = new Date();
            String startTime = sdf.format(startDate);
            System.out.println("Batch 开始时间为：" + startTime);
            System.out.println("Batch 开始执行...");

            // 使用PreparedStatement批量插入
            int idx = Initial.sqlBatchELPC(list);

            // 统计时间 - 结束
            System.out.println("Batch 执行完成，共插入" + idx + "条数据");
            Date endDate = new Date();
            String endTime = sdf.format(endDate);
            System.out.println("Batch 结束时间为：" + endTime);

            long start = startDate.getTime();
            long end = endDate.getTime();
            int minutes2 = (int) ((end - start) / (1000 * 60));
            System.out.println("两个时间之间的分钟差为：" + minutes2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 区划危险性数据
     */
    @GetMapping("/danger/saveBatch")
    public void saveBatchDanger() {
        try {
            // psBatch 时间统计 - 开始
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<FxVulnerabilityDangerEntity> list = initialService.listInitializeDanger();

            Date startDate = new Date();
            String startTime = sdf.format(startDate);
            System.out.println("Batch 开始时间为：" + startTime);
            System.out.println("Batch 开始执行...");

            // 使用PreparedStatement批量插入
            int idx = Initial.sqlBatchDanger(list);

            // 统计时间 - 结束
            System.out.println("Batch 执行完成，共插入" + idx + "条数据");
            Date endDate = new Date();
            String endTime = sdf.format(endDate);
            System.out.println("Batch 结束时间为：" + endTime);

            long start = startDate.getTime();
            long end = endDate.getTime();
            int minutes2 = (int) ((end - start) / (1000 * 60));
            System.out.println("两个时间之间的分钟差为：" + minutes2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 风险结果
     */
    @GetMapping("/risk/saveBatch")
    public void saveBatchRisk() {
        try {
            // psBatch 时间统计 - 开始
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<FxRiskResultEntity> list = initialService.listInitializeRisk();

            Date startDate = new Date();
            String startTime = sdf.format(startDate);
            System.out.println("Batch 开始时间为：" + startTime);
            System.out.println("Batch 开始执行...");

            // 使用PreparedStatement批量插入
            int idx = Initial.sqlBatchRisk(list);

            // 统计时间 - 结束
            System.out.println("Batch 执行完成，共插入" + idx + "条数据");
            Date endDate = new Date();
            String endTime = sdf.format(endDate);
            System.out.println("Batch 结束时间为：" + endTime);

            long start = startDate.getTime();
            long end = endDate.getTime();
            int minutes2 = (int) ((end - start) / (1000 * 60));
            System.out.println("两个时间之间的分钟差为：" + minutes2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
